package com.coding.base.util;

import com.coding.pojo.OssImages;
import com.coding.pojo.Video;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;

/**
 * @auther wangchenghao
 * @date 2020/7/25
 **/
public class CreateOssUtil {
    public static OssImages createOss(MultipartFile file) throws Exception {
        OssImages ossImages = new OssImages();
        if (file == null || file.getSize() <= 0) {
            return ossImages;
        }
        OSSClientUtil ossClient = new OSSClientUtil();
        String name = ossClient.uploadImg2Oss(file);
        String imgUrl = ossClient.getImgUrl(name);
        //填充信息
        ossImages.setImage_date(DateUtil.getStringDate());
        ossImages.setImage_url(imgUrl);
        ossImages.setImage_name(name);
        ossClient.destory();

        return ossImages;
    }

    public static Video createOssVideo(MultipartFile file) throws Exception {
        Video video = new Video();
        if (file == null || file.getSize() <= 0) {
            return video;
        }
        OSSClientUtil ossClient = new OSSClientUtil();
        String name = ossClient.uploadImg2Oss(file);
        String videoUrl = ossClient.getImgUrl(name);
        //填充信息
        video.setVideo_name(name);
        video.setVideo_url(videoUrl);
        ossClient.destory();
        return video;
    }

    public static List<OssImages> creatOssImages(MultipartFile[] files) throws Exception {
        List<OssImages> ossImagesList = new ArrayList<>();
        for (int i = 0; i < files.length; i++) {

            OssImages ossImages = new OssImages();
            if (files[i] == null || files[i].getSize() <= 0) {
                return ossImagesList;
            }
            OSSClientUtil ossClient = new OSSClientUtil();
            String name = ossClient.uploadImg2Oss(files[i]);
            String imgUrl = ossClient.getImgUrl(name);
            //填充信息
            ossImages.setImage_date(DateUtil.getStringDate());
            ossImages.setImage_url(imgUrl);
            ossImages.setImage_name(name);
            ossClient.destory();
            ossImagesList.add(ossImages);
        }
        return ossImagesList;
    }
}
